<?php
/**
 * User: Jacky Yu
 * Date: 2017/8/21
 * Time: 17:30
 * Description: 服务器命令行处理控制器
 */

class Cli extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        if(!is_cli()) exit('404');
    }

    public function index()
    {
        //$this->load->helper('url');
        //redirect(base_url('index.php/wap/index'));
    }

    /**
     * 每天凌晨1点汇总前一天的游戏历史记录数据，并转存到相对应的归档数据表
     * @param string $task_name 任务名
     */
    public function day_cron_task($task_name = 'all'){
        $this->load->model('visitlog_model');
        $this->load->model('archive_model');
        $yesterday = strtotime(date('Y-m-d',time())) - 24*3600;//汇总前一天数据

        if(in_array($task_name, ['all','game'])){
            //1、每天每个游戏的点击数
            $res_game = $this->visitlog_model->summarize_day_click_by_game($yesterday);
            $ret_game = $this->archive_model->add_batch($res_game, 'day_game_click');
            echo date('Y-m-d',$yesterday).'数据归档：成功添加汇总记录'.$ret_game.'条到17koko_day_game_click数据表'."\n";
        }

        if(in_array($task_name, ['all','channel'])){
            //2、各个渠道每日的点击数
            $res_game = $this->visitlog_model->summarize_day_click_by_channel($yesterday);
            $ret_game = $this->archive_model->add_batch($res_game, 'day_channel_click');
            echo date('Y-m-d',$yesterday).'数据归档：成功添加汇总记录'.$ret_game.'条到17koko_day_channel_click数据表'."\n";
        }

        if(in_array($task_name, ['all','hour'])){
            //3、每天各个小时的总访问量
            $res_game = $this->visitlog_model->summarize_hour_click_by($yesterday);
            $ret_game = $this->archive_model->add_batch($res_game, 'hour_total_click');
            echo date('Y-m-d',$yesterday).'数据归档：成功添加汇总记录'.$ret_game.'条到17koko_hour_total_click数据表'."\n";
        }
    }

    /**
     * 批量处理归档前的访问记录
     */
    public function batch_history_task(){
        $this->load->model('visitlog_model');
        $this->load->model('archive_model');
        $day = strtotime(date('Y-m-d',time())) - 2*24*3600;//从两天前开始往后汇总数据
        $res_game4 = 1;

        while (!empty($res_game4)){
            $res_game1 = $this->visitlog_model->summarize_day_click_by_game($day);
            $ret_game1 = $this->archive_model->add_batch($res_game1, 'day_game_click');
            echo date('Y-m-d',$day).' 数据归档：成功添加汇总记录'.$ret_game1.'条到17koko_day_game_click数据表'."\n";

            $res_game2 = $this->visitlog_model->summarize_day_click_by_channel($day);
            $ret_game2 = $this->archive_model->add_batch($res_game2, 'day_channel_click');
            echo date('Y-m-d',$day).' 数据归档：成功添加汇总记录'.$ret_game2.'条到17koko_day_channel_click数据表'."\n";;

            $res_game3 = $this->visitlog_model->summarize_hour_click_by($day);
            $ret_game3 = $this->archive_model->add_batch($res_game3, 'hour_total_click');
            echo date('Y-m-d',$day).' 数据归档：成功添加汇总记录'.$ret_game3.'条到17koko_hour_total_click数据表'."\n";;

            $day -= 24*3600;
            $res_game4 = $this->visitlog_model->summarize_hour_click_by($day);
        }
    }
}